package com.tq68.weixin.utils;

import com.jfinal.kit.HttpKit;
import com.jfinal.weixin.sdk.api.AccessToken;
import com.jfinal.weixin.sdk.api.AccessTokenApi;

public class JssdkTicketUtil {
	 
    private static JssdkTicket jssdkTicket;
 
    public static JssdkTicket getJssdkTicket() {
        if(jssdkTicket != null && jssdkTicket.isAvailable()) {
            return jssdkTicket;
        } else {
            refreshAccessToken();
            return jssdkTicket;
        }
    }
 
    private static void refreshAccessToken() {
        jssdkTicket = requestJssdkTicket();
    }
 
    private static synchronized JssdkTicket requestJssdkTicket() {
        JssdkTicket result = null;
        AccessToken accessToken = AccessTokenApi.getAccessToken();
        String json = HttpKit.get("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + accessToken.getAccessToken() + "&type=jsapi");
        result = new JssdkTicket(json);
        if(!result.isAvailable()) {
            refreshAccessToken();
        }
        return result;
    }
}
